package com.protocols;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

public final class ProtoOperations {

	public  static <T> byte[] concatArrays(byte[] first, byte[]... rest) {
		  int totalLength = first.length;
		  for (byte[] array : rest) {
		    totalLength += array.length;
		  }
		  byte[] result = Arrays.copyOf(first, totalLength);
		  int offset = first.length;
		  for (byte[] array : rest) {
		    System.arraycopy(array, 0, result, offset, array.length);
		    offset += array.length;
		  }
		  return result;
	}
	
	public static String getHexString( byte[] mybyte){
		
	    StringBuilder sb = new StringBuilder();
	    for (byte b : mybyte) {
	        sb.append(String.format("%02X ", (b&0xff)));
	    }
	   return sb.toString();

	    
	    

	}
	
	public static void  getWireSharkPcap( byte[] mybyte)throws IOException{
		
	    StringBuilder sb = new StringBuilder();
	    for (byte b : mybyte) {
	        sb.append(String.format("%02X ", b));
	    }
		    
	   FileWriter fstream = new FileWriter("F:\\Projects\\Google\\mihira\\hexstream.txt",false);
	   BufferedWriter out = new BufferedWriter(fstream);
	   out.write("00000000 "+sb.toString()+"'H\n");
	   out.close();
	   
	   Runtime.getRuntime().exec("c:\\Program Files\\Wireshark\\text2pcap.exe -l 150 F:\\Projects\\Google\\mihira\\hexstream.txt F:\\Projects\\Google\\mihira\\pdu.cap");
	   Runtime.getRuntime().exec("c:\\Program Files\\Wireshark\\wireshark F:\\Projects\\Google\\mihira\\pdu.cap");
	}
	
	public static void ProtocolDecoder(byte [] protocoldata){
		
		
	}
	
	public static int byteArrayToInt(byte[] by) 
	{
		int value = 0;
	    for (int i = 0; i < by.length; i++)
	    {
	       value = (value << 8) + (by[i] & 0xff);
	    }
	    return value;
	    
	    
	}

	
	public static long byteArrayToLong(byte[] by) 
	{
		long value = 0;
	    for (int i = 0; i < by.length; i++)
	    {
	       value = (value << 8) + (by[i] & 0xff);
	    }
	    return value;
	}
	
	public static byte[] extractByteArray(byte[] input, int startindex, int length) 
	{
		if (input.length<startindex || startindex<0 || length<0 ||(startindex+length)>input.length)
			return new byte[0];
		
		byte [] result = new byte [length];
		
		System.arraycopy(input, startindex, result, 0, length);
		
		return result;
	}
	
	public static byte[] ReverseByteArray(byte [] input){
		
		byte [] output= new byte[input.length];
		
		for(int i = 0; i < input.length; i++) {
			output[i] = input[input.length - 1 - i];
		}

		return output;
	}
	
	public static int intFromByteArray(byte[] input, int startindex, int length) 
	{
	
		byte [] result = new byte [length];
		
		System.arraycopy(input, startindex, result, 0, length);
				
		return byteArrayToInt(result);
	}

	public static long longFromByteArray(byte[] input, int startindex, int length) 
	{
	
		byte [] result = new byte [length];
		
		System.arraycopy(input, startindex, result, 0, length);
				
		return byteArrayToLong(result);
	}
	

	
	
	
	
}
